package org.walklink.walklinkserver.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.walklink.common.entity.DefaultTrackDto;
import org.walklink.common.util.*;
import org.walklink.core.event.EventHandlerStrategy;

/**
 * 服务端业务处理
 *
 * @author lidishan
 */
public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {

    private static final Logger LOGGER = LogManager.getLogger(UdpServerHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket datagramPacket) {
        DefaultTrackDto trackDto = JsonUtil.silentString2Object(datagramPacket.content().toString(CharsetUtil.UTF_8), DefaultTrackDto.class);
        if (trackDto == null) {
            LOGGER.error("UdpServerHandler channelRead0 trackDto is null");
            return;
        }
        if (trackDto.getData() == null) {
            LOGGER.error("UdpServerHandler channelRead0 trackDto data is null trackDto");
            return;
        }

        // TODO: 接收到消息的后置处理，根据事件类型处理
        // - 考虑到显示明显，所以用switch case来显示
        switch (trackDto.getTypeEnum()) {
            case HEARTBEAT:
                // todo:
                break;
            case TOPIC_QUERY:
                // todo:
                break;
            case JVM_SYN:
                EventHandlerStrategy.select(trackDto.getTypeEnum()).receive(trackDto);
                break;
            default:

        }
    }
}